/* ***********************************************************************
 * 项目名称:油耗
 * 项目组:
 * 创建时间:2017.7.11
 * 创建人:马方印
 * 用途:
 * 版本:1.0
 * 修改记录:    修改内容   修改原因   修改人   修改时间
 *               无        无       无       无
 *************************************************************************/

#import "YZRecordLitreCell.h"
#import "YZInputOrOutputGasolineModel.h"

@implementation YZRecordLitreCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self.contentView addSubview:self.backView];
        [self.backView addSubview:self.alertImageView];
        [self.backView addSubview:self.oilLabel];
        [self.backView addSubview:self.oilNumLabel];
        [self.contentView addSubview:self.topImageView];
        [self.contentView addSubview:self.midImageView];
        [self.contentView addSubview:self.bottomImageView];
        
        [self setSubViewsFrame];
    }
    
    return self;
}

#pragma mark - 数据源赋值
- (void)setSingleListModel:(YZSingleListModel *)singleListModel{
    _singleListModel = singleListModel;
    
    self.oilLabel.text = [NSString stringWithFormat:@"%.1fL",[singleListModel.iol floatValue]];
    self.oilNumLabel.text = [NSString stringWithFormat:@"%@",singleListModel.iolnum];
    
    if ([singleListModel.isException integerValue] == globalNum1) {//有报警
        
        [self.alertImageView setHidden:NO];
        
        [self.alertImageView.layer addAnimation:[self opacityForever_Animation:1] forKey:singleListModel.iolnum];
        
        [self.oilNumLabel mas_updateConstraints:^(MASConstraintMaker *make) {//空出闪烁图标的位置
            
            make.right.mas_equalTo(self.alertImageView.mas_left).with.offset(0);
        }];
        
    }else{//正常
        
        [self.alertImageView setHidden:YES];
        
        [self.alertImageView.layer removeAllAnimations];
        
        [self.oilNumLabel mas_updateConstraints:^(MASConstraintMaker *make) {//删除闪烁图标的位置
            
            make.right.mas_equalTo(self.alertImageView.mas_left).with.offset(21);
        }];
    }
}

#pragma mark - 布局
- (void)setSubViewsFrame{

    CGFloat w = kScreenWidth/3 + 20;//宽度
    
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.right.mas_equalTo(self.contentView.mas_right).with.offset(-10);
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.width.mas_equalTo(@(w));
        make.height.mas_equalTo(@35);
    }];
    
    [self.alertImageView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.right.mas_equalTo(self.backView.mas_right).with.offset(-5);
        make.centerY.mas_equalTo(self.backView.mas_centerY);
        make.width.mas_equalTo(@21);
        make.height.mas_equalTo(@21);
    }];

    [self.oilNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(self.alertImageView.mas_left).with.offset(0);
        make.centerY.mas_equalTo(self.backView.mas_centerY);
        make.width.mas_equalTo(@35);
        make.height.mas_equalTo(@35);
    }];
    
    [self.oilLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.right.mas_equalTo(self.oilNumLabel.mas_left).with.offset(-5);
        make.centerY.mas_equalTo(self.backView.mas_centerY);
        make.left.mas_equalTo(self.backView.mas_left).with.offset(5);
        make.height.mas_equalTo(@35);
    }];
    
    [self.midImageView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.right.mas_equalTo(self.backView.mas_left);
        make.centerY.mas_equalTo(self.backView.mas_centerY);
        make.width.mas_equalTo(@(w*0.5 - 20));
        make.height.mas_equalTo(@2);
    }];
    
    [self.topImageView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.contentView.mas_top);
        make.left.mas_equalTo(self.midImageView.mas_left);
        make.bottom.mas_equalTo(self.midImageView.mas_top);
        make.width.mas_equalTo(@2);
    }];
    
    [self.bottomImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.midImageView.mas_bottom);
        make.left.mas_equalTo(self.midImageView.mas_left);
        make.bottom.mas_equalTo(self.contentView.mas_bottom);
        make.width.mas_equalTo(@2);
    }];
}

#pragma mark - 懒加载
- (UIImageView *)alertImageView{
    
    if (!_alertImageView) {
        
        _alertImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"notice"]];
        
        [_alertImageView setHidden:YES];
    }
    
    return _alertImageView;
}

- (UILabel *)oilNumLabel{
    
    if (!_oilNumLabel) {
        
        _oilNumLabel = [[UILabel alloc]init];
        _oilNumLabel.text = @"3罐";
        _oilNumLabel.font = [UIFont systemFontOfSize:16.0f];
        _oilNumLabel.textColor = COLOR(0, 153, 204, 1);
        _oilNumLabel.textAlignment = NSTextAlignmentCenter;
    }
    
    return _oilNumLabel;
}

- (UILabel *)oilLabel{
    
    if (!_oilLabel) {
        
        _oilLabel = [[UILabel alloc]init];
        _oilLabel.text = @"0L";
        _oilLabel.font = [UIFont systemFontOfSize:16.0f];
        _oilLabel.textColor = COLOR(0, 153, 204, 1);
        _oilLabel.textAlignment = NSTextAlignmentLeft;
    }
    
    return _oilLabel;
}

- (UIView *)backView{
    
    if (!_backView) {
        
        _backView = [[UIView alloc]init];
        _backView.backgroundColor = COLOR(238, 238, 238, 1);
        _backView.layer.cornerRadius = 4.0f;
        _backView.layer.masksToBounds = YES;
    }
    
    return _backView;
}
- (UIView *)bottomImageView{
    
    if (!_bottomImageView) {
        
        _bottomImageView = [[UIView alloc]init];
        _bottomImageView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"vertical_point"]];
    }
    
    return _bottomImageView;
}

- (UIView *)midImageView{
    
    if (!_midImageView) {
        
        _midImageView = [[UIView alloc]init];
        _midImageView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"honrizontal_point"]];
    }
    
    return _midImageView;
}

- (UIView *)topImageView{
    
    if (!_topImageView) {
        
        _topImageView = [[UIView alloc]init];
        _topImageView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"vertical_point"]];
    }
    
    return _topImageView;
}

#pragma mark === 永久闪烁的动画 ======
-(CABasicAnimation *)opacityForever_Animation:(float)time
{
    if (!_animation) {
        _animation = [CABasicAnimation animationWithKeyPath:@"opacity"];//必须写opacity才行。
        _animation.fromValue = [NSNumber numberWithFloat:1.0f];
        _animation.toValue = [NSNumber numberWithFloat:0.0f];//这是透明度。
        _animation.autoreverses = YES;
        _animation.duration = time;
        _animation.repeatCount = MAXFLOAT;
        _animation.removedOnCompletion = NO;
        _animation.fillMode = kCAFillModeForwards;
        //    animation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];///没有的话是均匀的动画。
    }
    return _animation;
}

@end
